package com.baturu.vin.honda.cleanData.baobao;

import com.baturu.vin.honda.dal.dao.HondaTestDAO;
import com.baturu.vin.test.AbstractBaseCase;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

/**
 * 本田报报
 * @Author: jiangxinlei
 * @Time: 2019-05-28 18:51
 **/
public class FetchFromHonda extends AbstractBaseCase {

    protected static String HONDA_URL = "http://hondai.lailian.net/Service1.svc";

    @Autowired
    private HondaTestDAO testDAO;

    @Test
    public void tt() throws Exception{
        List<HondaBaoBaoDTO> hondaBaoBaoDTOS = queryAllNpl();
        int count = 1;
        int size = hondaBaoBaoDTOS.size();
        for (int i = 71199; i< size; i++) {
            HondaBaoBaoDTO p = hondaBaoBaoDTOS.get(i);
            System.out.println("正在执行第"+count+"/"+size+"条,其中npl:"+p.getNpl()+" nplblk:"+p.getNplblk());
            count++;
            //这几个已经验证了是没数据的
            if (Sets.newHashSet("13S0201","13S0301","13S0401","13S0411").contains(p.getNpl())) {
                continue;
            }
            fetchFromBaoBao(p.getNpl(), p.getNplblk());
        }
    }

    private List<HondaBaoBaoDTO> queryAllNpl() {
        return testDAO.queryAllNpl();
    }

    private void fetchFromBaoBao(String npl, String nplblk) throws Exception{
        if (StringUtils.isBlank(npl) || StringUtils.isBlank(nplblk)) {
            return;
        }
        Map<String, String> headerParam = Maps.newHashMap();
        headerParam.put("Accept-Encoding","gzip, deflate");
        headerParam.put("Expect","100-continue");
        headerParam.put("Host","hondai.lailian.net");
        headerParam.put("Content-Type","application/soap+xml; charset=utf-8");

        String requestBody = "<s:Envelope xmlns:s=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:a=\"http://www.w3.org/2005/08/addressing\"><s:Header><a:Action s:mustUnderstand=\"1\">http://tempuri.org/IService1/T_MuLu</a:Action><a:MessageID>urn:uuid:7b82c618-687e-47e0-bb20-5db1b0c116a9</a:MessageID><a:ReplyTo><a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address></a:ReplyTo><a:To s:mustUnderstand=\"1\">http://hondai.lailian.net/Service1.svc</a:To></s:Header><s:Body><T_MuLu xmlns=\"http://tempuri.org/\"><condition> (((t_MuLu.MuLu)='"+npl+"') AND ((t_MuLu.TuPian)='"+nplblk+"'))</condition><strYingPan>1F8BFBFF000906EA|1343115277|00:0C:29:CF:C0:A3|6161332562322242932324211191611813</strYingPan></T_MuLu></s:Body></s:Envelope>";
        Document document = Jsoup.connect(HONDA_URL).headers(headerParam).requestBody(requestBody).post();

        Elements elementsByTag = document.getElementsByTag("b:t_MuLu");
        if (elementsByTag.size()<=0) {
            return;
        }

        List<HondaBaoBaoDTO> ll = Lists.newArrayList();
        for (Element element : elementsByTag) {
            Elements allElements = element.getAllElements();
            HondaBaoBaoDTO hondaBaoBaoDTO = new HondaBaoBaoDTO();
            for (Element tiny : allElements) {
                String tagName = tiny.tagName();
                switch (tagName) {
                    case "b:_chejiajieshu":{
                        hondaBaoBaoDTO.setChejiajieshu(dealNull(tiny, tagName));
                        continue;
                    }
                    case "b:_chejiakaishi":{
                        hondaBaoBaoDTO.setChejiakaishi(dealNull(tiny, tagName));
                        continue;
                    }
                    case "b:_lingjianhao":{
                        hondaBaoBaoDTO.setPartsCode(dealNull(tiny, tagName));
                        continue;
                    }
                    case "b:_lingjianming":{
                        hondaBaoBaoDTO.setEpcPartsNameEN(dealNull(tiny, tagName));
                        continue;
                    }
                    case "b:_lingjianming_cn":{
                        hondaBaoBaoDTO.setEpcPartsNameCN(dealNull(tiny, tagName));
                        continue;
                    }
                    case "b:_mulu":{
                        hondaBaoBaoDTO.setNpl(dealNull(tiny, tagName));
                        continue;
                    }
                    case "b:_tuhao":{
                        hondaBaoBaoDTO.setTuhao(dealNull(tiny, tagName));
                        continue;
                    }
                    case "b:_tupian":{
                        hondaBaoBaoDTO.setNplblk(dealNull(tiny, tagName));
                        continue;
                    }
                    case "b:_xuhao":{
                        hondaBaoBaoDTO.setXuhao(dealNull(tiny, tagName));
                        continue;
                    }
                    case "b:_yongliang":{
                        hondaBaoBaoDTO.setYongliang(dealNull(tiny, tagName));
                        continue;
                    }
                    default: {

                    }
                }
            }
            ll.add(hondaBaoBaoDTO);
        }
        //
        insert(ll);
    }

    private void insert(List<HondaBaoBaoDTO> ll) throws Exception{
        if (CollectionUtils.isEmpty(ll)) {
            return;
        }
        
        testDAO.insertBaoBao(ll);

//        String collect = ll.stream().map(p ->
//                    "('" +
//                    dealNull(p.getChejiakaishi()) + "','" +
//                    dealNull(p.getChejiajieshu()) + "','" +
//                    dealNull(p.getPartsCode()) + "','" +
//                    dealNull(p.getEpcPartsNameEN()) + "','" +
//                    dealNull(p.getEpcPartsNameCN()) + "','" +
//                    dealNull(p.getNpl()) + "','" +
//                    dealNull(p.getTuhao()) + "','" +
//                    dealNull(p.getNplblk()) + "','" +
//                    dealNull(p.getXuhao()) + "','" +
//                    dealNull(p.getYongliang()) +
//                    "')"
//        ).collect(Collectors.joining(","));
//
//        System.out.println(collect);

//        Class.forName("com.mysql.jdbc.Driver");
//        Connection conn = DriverManager.getConnection(dburl, uname, pw);
//        String insertSql = "insert into honda_parts_desc_cn_tt(`chejiakaishi`, `chejiajieshu`, `partsCode`, `epcPartsNameEN`, `epcPartsNameCN`, `NPL`, `tuhao`, `NPLBLK`, `xuhao`, `yongliang`) values "
//                +collect;
//
//        Statement stmt = conn.createStatement();
//        boolean rs = stmt.execute(insertSql);
//        stmt.close();
//        conn.close();
    }

    private String dealNull(Element tiny, String tagName) {
        if (tiny.getElementsByTag(tagName).get(0).childNodes().size()<=0) {
            return StringUtils.EMPTY;
        }
        String str = tiny.getElementsByTag(tagName).get(0).childNodes().get(0).toString();
        if (StringUtils.isBlank(str)) {
            return StringUtils.EMPTY;
        }

        return str.replaceAll("\n","");
    }

}
